package com.woniuxy.cloud.starter.context;


import com.ostrich.cloud.common.CommonConstants;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class ContextPopulateFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String operatorIdStr = request.getHeader(CommonConstants.CONTEXT_OPERATOR_ID_KEY);
        String operatorName = request.getHeader(CommonConstants.CONTEXT_OPERATOR_NAME_KEY);
        if(operatorIdStr!=null&&operatorName!=null) {
            Integer operatorId = Integer.valueOf(operatorIdStr);
            ContextInfoHolder.setContextInfo(new ContextInfo(operatorId, operatorName));
        }
        filterChain.doFilter(request,response);
    }
}
